package band.wukong.practice.webservice.kh.sample05;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jws.HandlerChain;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wukong(wukonggg@139.com)
 */
@WebService(
    // targetNamespace要和接口一致，因为创建server的时候用的是impl。
//    targetNamespace = "http://www.wukong.band/wkp/webservice/kh/sample05",
    endpointInterface = "band.wukong.practice.webservice.kh.sample05.JukeboxService"
)
@HandlerChain(file = "kh/sample05/handler-chain.xml")
public class JukeboxServiceImpl implements JukeboxService {

    private final Logger logger = LoggerFactory.getLogger(JukeboxServiceImpl.class);

    private static final List<Artist> artists = new ArrayList<>();

    JukeboxServiceImpl() {
        artists.add(new Artist("1", "Andy Timmons"));
        artists.add(new Artist("2", "Eric Clapton"));
        artists.add(new Artist("3", "Vinne Moore"));
    }

    @Override
    public String randomArtist(int genre) {
        if (genre < 0) throw new IllegalArgumentException();

        String artist = null;
        switch (genre) {
            case MusicConst.GENRE_CLASSIC:
                artist = "Cecilia Bartoli";
                break;
            case MusicConst.GENRE_ROCK:
                artist = "Bruce Springsteen";
                break;
            case MusicConst.GENRE_JAZZ:
                artist = "Miles Davis";
                break;
            case MusicConst.GENRE_BLUES:
                artist = "Eric Clapton";
                break;
            case MusicConst.GENRE_METAL:
                artist = "Metallica";
                break;
            case MusicConst.GENRE_POP:
                artist = "Michael Jackson";
                break;
            case MusicConst.GENRE_FUSION:
                artist = "Norah Jones";
                break;
            default:
                artist = "John Lennon";
                break;
        }

        logger.debug(artist);
        return artist;
    }

    @Override
    public Artist addArtist(String id, String name) {
        logger.debug("id = " + id + ", name = " + name);
        Artist artist = new Artist(id, name);
        artists.add(artist);
        return artist;
    }

    @Override
    public List<Artist> listArtists() {
        return artists;
    }

    @Override
    public void updateArtist(Artist artist, String authInfo) {
        logger.debug("artist = " + artist);
        logger.debug("authInfo = " + authInfo);
    }

    @Override
    public void removeArtist(Artist artist) throws JukeboxException {
        logger.debug("artist = " + artist);
        if (!artists.contains(artist)) {
            throw new JukeboxException("对象不存在！");
        }
        artists.remove(artist);
    }
}
